package com.xy.blog.user.service.impl;

import com.xy.blog.core.exception.BaseException;
import com.xy.blog.core.service.IBaseService;
import com.xy.blog.core.springmvc.result.page.PageReq;
import com.xy.blog.core.springmvc.result.page.PageResp;
import com.xy.blog.user.controller.model.req.query.SysDictQuery;
import com.xy.blog.user.dao.impl.SysDictDaoImpl;
import com.xy.blog.user.dao.model.SysDictDO;
import com.xy.blog.user.mapstruct.SysDictConverter;
import com.xy.blog.user.service.ISysDictService;
import com.xy.blog.user.service.model.SysDictBO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author zanhonglei
 * @version V1.0
 * @Description:
 * @date 2020/3/5
 */
@Service
public class SysDictServiceImpl implements IBaseService<SysDictQuery, SysDictBO> , ISysDictService {

    @Autowired
    private SysDictDaoImpl sysDictDaoImpl;

    @Autowired
    private SysDictConverter sysDictConverter;

    @Override
    public PageResp<SysDictBO> listByPage(PageReq<SysDictQuery> req) throws BaseException {
        return sysDictConverter.do2Bo(sysDictDaoImpl.listByPage(req));
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void save(SysDictBO item) throws BaseException {
        SysDictDO sysDictDO = sysDictConverter.bo2Do(item);
        sysDictDaoImpl.save(sysDictDO);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void update(SysDictBO item) throws BaseException {
        SysDictDO sysDictDO = sysDictConverter.bo2Do(item);
        sysDictDaoImpl.updateById(sysDictDO);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void deleteById(Long id) throws BaseException {
        sysDictDaoImpl.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    @Cacheable(cacheNames = "sys-dict",key="#id")
    public SysDictBO getById(Long id) throws BaseException {
        return sysDictConverter.do2Bo(sysDictDaoImpl.getById(id));
    }

    @Override
    public List<SysDictBO> listAll(String type) {
        SysDictDO sysDictDO = new SysDictDO();
        sysDictDO.setType(type);
        return sysDictConverter.do2Bo(sysDictDaoImpl.select(sysDictDO));
    }
}
